//
//  QXLable.m
//  TicketPlan
//
//  Created by tangang on 2017/12/21.
//  Copyright © 2017年 tangang. All rights reserved.
//

#import "QXLable.h"

@interface QXLable()
@property (nonatomic,strong)NSTextStorage *textStorage;
@property (nonatomic,strong)NSLayoutManager *layoutManager;
@property (nonatomic,strong)NSTextContainer *textContainer;
@end

@implementation QXLable

- (instancetype)init{
    if (self=[super init]) {
        self.textColor=DEFAULTTEXTBLACKCOLOR;
        self.font=[UIFont systemFontOfSize:13];
        self.numberOfLines=0;
        
    }
    return self;
}

- (void)use{
    self.userInteractionEnabled=YES;
    self.textStorage = [NSTextStorage new];
    self.layoutManager = [NSLayoutManager new];
    self.textContainer = [NSTextContainer new];
    [self.textStorage addLayoutManager:self.layoutManager];
    [self.layoutManager addTextContainer:self.textContainer];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onclickLab:)];
    [self addGestureRecognizer:tap];
}

- (instancetype)initCenterTextWithText:(NSString *)text{
    self=[self init];
    self.textAlignment=NSTextAlignmentCenter;
    self.text=text;
    return self;
}

- (instancetype)initTitle{
    self=[self init];
    self.font=[UIFont systemFontOfSize:15];
    return self;
}

- (instancetype)initWithFount:(NSInteger)fount{
    self=[self init];
    self.font=[UIFont systemFontOfSize:fount];
    self.textColor=DEFAULTTEXTGRAYCOLOR;
    return self;
}

- (instancetype)initWithBFount:(NSInteger)fount{
    self=[self init];
    self.font=[UIFont boldSystemFontOfSize:fount];
    return self;
}

- (instancetype)initGaryTitle{
    self=[self init];
    self.font=[UIFont systemFontOfSize:15];
    self.textColor=DEFAULTTEXTGDARKRAYCOLOR;
    return self;
}

- (instancetype)initGaryText{
    self=[self init];
    self.textColor=DEFAULTTEXTGDARKRAYCOLOR;
    return self;
}

-(void)setLabelSpace:(CGFloat)lineSpacing{
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paraStyle.lineSpacing = lineSpacing; //设置行间距
    paraStyle.alignment=NSTextAlignmentCenter;
    NSDictionary *dic = @{NSFontAttributeName:self.font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.0f
                          };
    NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:self.text attributes:dic];
    self.attributedText = attributeStr;
}

-(void)setSpace:(CGFloat)lineSpacing{
    
    if (self.text.length==0) {
        return;
    }
    
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paraStyle.lineSpacing = lineSpacing; //设置行间距
    NSDictionary *dic = @{NSFontAttributeName:self.font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.0f
                          };
    NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:self.text attributes:dic];
    self.attributedText = attributeStr;
}

-(void)setRightSpace:(CGFloat)lineSpacing{
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paraStyle.lineSpacing = lineSpacing; //设置行间距
    paraStyle.alignment=NSTextAlignmentRight;
    NSDictionary *dic = @{NSFontAttributeName:self.font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.0f
                          };
    NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:self.text attributes:dic];
    self.attributedText = attributeStr;
}


- (void)onclickLab:(UITapGestureRecognizer *)tap{
    if (!self.selectBlock) {
        return;
    }
    CGPoint location = [tap locationInView:self];
    self.textContainer.size = self.bounds.size;
    self.textContainer.lineFragmentPadding = 0;
    self.textContainer.maximumNumberOfLines = self.numberOfLines;
    self.textContainer.lineBreakMode = self.lineBreakMode;
    CGFloat fontPointSize = self.font.pointSize;
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
    NSRange textRange = NSMakeRange(0, attributedText.length);
    [attributedText addAttribute:NSFontAttributeName value:self.font range:textRange];
//    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
//    paragraphStyle.alignment = self.textAlignment;
//    paragraphStyle.lineSpacing = (fontPointSize==17?12:0);
//    [attributedText addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:textRange];
    [self.textStorage setAttributedString:attributedText];
    
    CGSize textSize = [self.layoutManager usedRectForTextContainer:self.textContainer].size;
    //    location.x -= (CGRectGetWidth(self.label.frame) - textSize.width) / 2;
    location.y -= ((CGRectGetHeight(self.frame) - textSize.height) / 2);
    
    NSUInteger glyphIndex = [self.layoutManager glyphIndexForPoint:location inTextContainer:self.textContainer];
    [self.layoutManager setAttachmentSize:CGSizeMake(fontPointSize, fontPointSize) forGlyphRange:NSMakeRange(self.text.length - 1, 1)];
    NSAttributedString *attributedSubstring = [self.attributedText attributedSubstringFromRange:NSMakeRange(glyphIndex, 1)];
    CGRect glyphRect = [self.layoutManager boundingRectForGlyphRange:NSMakeRange(glyphIndex, 1)
                                                     inTextContainer:self.textContainer];
    if (!CGRectContainsPoint(glyphRect, location)) {
        self.selectBlock(glyphIndex,attributedSubstring);
        return;
    }
    self.selectBlock(glyphIndex,attributedSubstring);
}

@end
